/**
 * 
 */
package com.hs.job.task;

import java.util.List;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.hs.HsBase;
import com.hs.core.db.DBOperator;
import com.hs.core.db.DatabaseConnection;
import com.hs.core.index.IndexFacade;
import com.hs.pre.model.JobEntry;
import com.hs.pre.model.PreEntry;

/**
 * @author shl
 *
 */
public class IndexerSearchFile implements Job{

	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		try {
			DBOperator dbo = this.getDBOperator();
			String sql = HsBase.getAnsiSqlMap().get("pre_job.select_bytype")+"";
			List jobList = dbo.select(sql, new Object[]{JobEntry.JOB_STATUS_ONE,JobEntry.JOB_TYPE_ONE});
			if(null != jobList){
				IndexFacade indexer = new IndexFacade();
				//indexer.indexPre(entry))
				for(int i = 0;i<jobList.size();i++){
					Map m = (Map)jobList.get(i);
					String preSql = HsBase.getAnsiSqlMap().get("preinfo.validate")+""; 
					JobEntry entry = new JobEntry(m);
					long start = System.currentTimeMillis();
					List preList = dbo.select(preSql, new Object[]{entry.getRelationId()});
					if(preList.size()>0){
						PreEntry preEntry = new PreEntry((Map)preList.get(0));
						indexer.indexPre(preEntry);
					}
					Class func = null;
				    func = Class.forName(entry.getExeClass());
				    TaskFunction task = (TaskFunction)func.newInstance();
				    task.setJob(entry);
				    task.execute();
				    long end = System.currentTimeMillis();
				    String time = (start - end)/1000+"";
				    entry.setJobExeTime(time);
				    //更新任务执行状态
				    entry.updateJob();
				}
			}
		} catch (ClassNotFoundException e) { 
			e.printStackTrace();
		} catch (InstantiationException e) { 
			e.printStackTrace();
		} catch (IllegalAccessException e) { 
			e.printStackTrace();
		}
	}
	/**
	 * 获得数据库操作类
	 * @return
	 */
	protected DBOperator getDBOperator(){
		try { 
			DBOperator dbo = DatabaseConnection.getDBOperator();
			return dbo;
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return null;
	}

}
